﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MillerApp.UserControls.Enums;
using MillerApp.DataContract.Common;

namespace MillerApp.UserControls
{
    public partial class QueryActions : UserControl
    {
        public QueryActions()
        {
            InitializeComponent();
            this.Load += new EventHandler(QueryActions_Load);
        }

        void QueryActions_Load(object sender, EventArgs e)
        {
            this.ParentForm.AcceptButton = this.btnQuery;
        }
        /// <summary>
        /// 在查询数据时触发此事件，一般用于从服务器加载数据
        /// </summary>
        public event QueryingDataEventHandler OnQueryingData;
        public event GetStringDelegate GetConditions;
        public event GetSortExpression GetSortExpression;
        private DataPagerControl _pagerControl = null;
        public DataPagerControl PagerControl
        {
            get { return _pagerControl; }
            set 
            {
                this._pagerControl = value;
                if (this._pagerControl != null)
                {
                    this._pagerControl.PagerChangeEvent += new DelegateMessageHanldler(pagerControl_PagerChangeEvent);
                }
            }
        }
        private string _currentSortColumn = "1";
        public string CurrentSortColumn
        {
            get { return _currentSortColumn; }
        }

        private SortDirection _currentSortDirection = SortDirection.ASC;
        public SortDirection CurrentSortDirection
        {
            get { return _currentSortDirection; }
        }

        object pagerControl_PagerChangeEvent(Dictionary<string, object> args)
        {
            OnQueryData();
            return new object();
        }

        private bool initPager = false;

        private int recorderCount = 0;
        public int RecorderCount
        {
            get { return recorderCount; }
            set 
            {
                recorderCount = value;
                InitPager();
            }
        }

        private void InitPager()
        {
            if (PagerControl != null && initPager==false)
            {
                this.PagerControl.IniDataPager(recorderCount);
                initPager = true;
            }
        }
 
        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (PagerControl != null)
            {
                PagerControl.PageIndex=0;
            }
            OnQueryData();
        }

        public void OnQueryData()
        {
            int pageIndex = 0;
            int pageSize = 100;

            string conditions = string.Empty;
            SortExpression sortExpression2;
            string sortExpression = string.Empty;
            if (GetConditions != null)
            {
                conditions = GetConditions();
            }
            if (GetSortExpression != null)
            {
                sortExpression2 = GetSortExpression();
                if (sortExpression2 != null&&string.IsNullOrEmpty(sortExpression2.SortColumn)==false)
                {
                    _currentSortColumn = sortExpression2.SortColumn;
                    _currentSortDirection = sortExpression2.SortDirection;
                    sortExpression = sortExpression2.SortColumn + " " + sortExpression2.SortDirection.ToString();
                }
            }
            if (PagerControl != null)
            {
                pageSize = PagerControl.PageSize;
                pageIndex = PagerControl.PageIndex;
            }
            if (pageIndex <= 0)
            {
                initPager = false;
            }
            else
            {
                initPager = true;
            }
            if (OnQueryingData != null)
            {
                OnQueryingData(conditions, sortExpression, pageIndex, pageSize);
            }
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {

        }
    }

    /// <summary>
    /// 查询数据委托
    /// </summary>
    /// <param name="conditions">查询条件</param>
    /// <param name="sortExpression">排序表达式</param>
    /// <param name="pageIndex">页码</param>
    /// <param name="pageSize">每页行数</param>
    public delegate void QueryingDataEventHandler(string conditions, string sortExpression, int pageIndex, int pageSize);

    /// <summary>
    /// 获取字符串（没有参数的委托）
    /// </summary>
    /// <returns></returns>
    public delegate string GetStringDelegate();

    public delegate SortExpression GetSortExpression();
}
